{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1f913851-93b5-480a-aafc-6b02fc11be84",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入字符串 \"111\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['111']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#定义\n",
    "def string_escape(code):\n",
    "    strings = []  # 存储提取的字符串\n",
    "    current_string = \"\"  # 当前正在提取的字符串\n",
    "    i = 0  # 字符串的位置\n",
    "    length = len(code)\n",
    "    state = \"Initial\"  # 初始状态\n",
    "    while i < length:\n",
    "        char = code[i]\n",
    "\n",
    "        if state == \"Initial\":\n",
    "            if char == '\"':\n",
    "                # 遇到双引号，开始读取字符串\n",
    "                state = \"Reading String\"\n",
    "                current_string = \"\"  # 清空当前字符串\n",
    "        elif state == \"Reading String\":\n",
    "            if char == '\"':\n",
    "                # 遇到双引号，结束字符串\n",
    "                strings.append(current_string)\n",
    "                state = \"Initial\"\n",
    "            elif char == '\\\\':\n",
    "                # 遇到反斜杠，处理转义字符\n",
    "                state = \"Handling Escape\"\n",
    "            else:\n",
    "                # 继续读取字符\n",
    "                current_string += char\n",
    "        elif state == \"Handling Escape\":\n",
    "            # 处理转义字符后的下一个字符\n",
    "            current_string += char\n",
    "            state = \"Reading String\"\n",
    "        # 移动到下一个字符\n",
    "        i += 1\n",
    "    return strings\n",
    "code=input(\"请输入字符串\")\n",
    "string_escape(code)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
